﻿using System;

using System.Collections;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Global.Class.Library;
using BusinessLogicLayer;
using BusinessLogicLayer.MetaDataList;
using DataAccessLayer;
using DataAccessLayer.MetaDataList;
using DataAccessLayer.MetaDataList.ListSalesmenNameDTSTableAdapters;
using DataTransferObject;
using DataTransferObject.MetaDataList;
using System.Globalization;
using BrightIdeasSoftware;
using Equin.ApplicationFramework;



namespace PresentationLayer
{
    public partial class ListSalesmenName : Form, IMergeToolStrip, ICallToolStrip
    {

        #region <Implement Interface>

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public GlobalEnum.TaskID TaskID
        {
            get { return this.listSalesmenNameBLL.TaskID; }
        }


        public ToolStrip ChildToolStrip
        {
            get
            {
                return this.toolStripChildForm;
            }
            set
            {
                this.toolStripChildForm = value;
            }
        }





        #region Context toolbar


        public bool IsDirty
        {
            get { return this.listSalesmenNameBLL.IsDirty; }
        }

        public bool IsValid
        {
            get { return this.listSalesmenNameBLL.IsValid; }
        }

        public bool Closable
        {
            get { return true; }
        }

        public bool Loadable
        {
            get { return true; }
        }

        public bool Newable
        {
            get { return !this.listSalesmenNameBLL.ReadOnly; }
        }

        public bool Editable
        {
            get { return !this.listSalesmenNameBLL.ReadOnly; }
        }

        public bool Deletable
        {
            get { return this.listSalesmenNameBLL.Editable; }
        }

        public bool Importable
        {
            get { return false; }
        }

        public bool Exportable
        {
            get { return false; }
        }

        public bool Verifiable
        {
            get { return this.listSalesmenNameBLL.Verifiable; }
        }

        public bool Unverifiable
        {
            get { return this.listSalesmenNameBLL.Unverifiable; }
        }

        public bool Printable
        {
            get { return false; }
        }

        public bool Searchable
        {
            get { return true; }
        }

        /// <summary>
        /// Edit Mode: True, Read Mode: False
        /// </summary>
        private bool editableMode;
        private int lastSalesmenNameID;
        public bool EditableMode { get { return this.editableMode; } }
        /// <summary>
        /// This reverse of the EditableMode
        /// </summary>
        public bool ReadonlyMode { get { return !this.editableMode; } }

        /// <summary>
        /// Set the editableMode
        /// </summary>
        /// <param name="editableMode"></param>
        private void SetEditableMode(bool editableMode)
        {
            if (this.editableMode != editableMode)
            {
                this.lastSalesmenNameID = this.listSalesmenNameBLL.SalesmenID;
                this.editableMode = editableMode;
                this.NotifyPropertyChanged("EditableMode");

                this.naviGroupDetails.Expanded = editableMode;
            }
        }


        private void CancelDirty(bool restoreSavedData)
        {
            try
            {
                if (restoreSavedData || this.listSalesmenNameBLL.SalesmenID <= 0)
                    this.listSalesmenNameBLL.Fill(this.lastSalesmenNameID);

                this.SetEditableMode(false);
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }



        #endregion Context toolbar



        #region ICallToolStrip

        public void Escape()
        {
            if (this.EditableMode)
            {
                if (this.IsDirty)
                {
                    DialogResult dialogResult = MessageBox.Show(this, "Do you want to save your change?", "Warning", MessageBoxButtons.YesNoCancel);
                    if (dialogResult == DialogResult.Yes)
                    {
                        this.Save();
                        if (!this.IsDirty) this.CancelDirty(false);
                    }
                    else if (dialogResult == DialogResult.No)
                    {
                        this.CancelDirty(true);
                    }
                    else
                        return;
                }
                else
                    this.CancelDirty(false);
            }
            else
                this.Close(); //Unload this module
        }


        public void Loading()
        {
            this.GetMasterList();
        }

        public void New()
        {
            this.listSalesmenNameBLL.New();
            this.SetEditableMode(true);
        }

        public void Edit()
        {
            this.SetEditableMode(true);
            if (this.checkBoxSalesmenCommodityAll.Checked == true)
            {
                this.dataGridViewListCommodity.ReadOnly = true;
                this.dataGridViewListItemClass.ReadOnly = true;
            }
        }

        public void Save()
        {
            this.listSalesmenNameBLL.Save();
            this.GetMasterList();
        }

        public void Delete()
        {
            DialogResult dialogResult = MessageBox.Show(this, "Are you sure you want to delete " + this.listSalesmenNameBLL.ListSalesmenNameMaster.Description + "?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Stop);
            if (dialogResult == DialogResult.Yes)
            {
                try
                {
                    this.listSalesmenNameBLL.Delete();
                    this.GetMasterList();
                }
                catch (Exception exception)
                {
                    GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
                }
            }
        }

        public void Import()
        {
            ImportExcel();
        }

        public void Export()
        {
            try
            {
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }

        public void Verify()
        {
            MessageBox.Show("Verify");
        }

        public void Print(PrintDestination printDestination)
        {
            MessageBox.Show("Print");
        }

        public void SearchText(string searchText)
        {

        }

        #endregion


        #endregion <Implement Interface>








        #region <Storage>
        private ListSalesmenNameBLL listSalesmenNameBLL;
    

        #endregion <Storage>


        public ListSalesmenName()
        {
            try
            {
                InitializeComponent();

                this.listSalesmenNameBLL = new ListSalesmenNameBLL();

                this.listSalesmenNameBLL.PropertyChanged += new PropertyChangedEventHandler(listSalesmenNameBLL_PropertyChanged);

            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }

        void listSalesmenNameBLL_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            this.NotifyPropertyChanged(e.PropertyName);
        }


        private void ListSalesmenName_Load(object sender, EventArgs e)
        {
            try
            {
                InitializeTabControl();

                InitializeCommonControlBinding();

                InitializeDataGridBinding();

                InitializeReadOnlyModeBinding();
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }


        private void InitializeTabControl()
        {
            try
            {
                CustomTabControl customTabControlSalesmenName = new CustomTabControl();

                customTabControlSalesmenName.ImageList = this.imageListTabControl;

                customTabControlSalesmenName.TabPages.Add("Territories", "Territories    ", 5);
                customTabControlSalesmenName.TabPages[0].Controls.Add(this.dataGridViewListAddressArea);
                this.dataGridViewListAddressArea.Dock = DockStyle.Fill;

                customTabControlSalesmenName.TabPages.Add("Customers", "Customers    ", 0);
                customTabControlSalesmenName.TabPages[1].Controls.Add(this.dataGridViewListCustomer);
                this.dataGridViewListCustomer.Dock = DockStyle.Fill;

                customTabControlSalesmenName.TabPages.Add("ItemClass", "Item Class    ", 4);
                customTabControlSalesmenName.TabPages[2].Controls.Add(this.dataGridViewListItemClass);
                this.dataGridViewListItemClass.Dock = DockStyle.Fill;

                customTabControlSalesmenName.TabPages.Add("Commodities", "Commodities    ", 9);
                customTabControlSalesmenName.TabPages[3].Controls.Add(this.dataGridViewListCommodity);
                this.dataGridViewListCommodity.Dock = DockStyle.Fill;

                customTabControlSalesmenName.DisplayStyle = TabStyle.VisualStudio;
                customTabControlSalesmenName.DisplayStyleProvider.ImageAlign = ContentAlignment.MiddleLeft;
                this.naviGroupDetails.Controls.Add(customTabControlSalesmenName);
                this.naviGroupDetails.Controls.SetChildIndex(customTabControlSalesmenName, 0);
                customTabControlSalesmenName.Dock = DockStyle.Fill;


            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }


        #region <InitializeBinding>


        Binding salesmenTitleIDBinding;
        Binding salesmenCategoryIDBinding;
        Binding salesmenTypeIDBinding;
       
        Binding descriptionBinding;
        Binding descriptionZULBinding;
        Binding descriptionDEIBinding;
        Binding descriptionOfficialBinding;
        Binding descriptionLastNameBinding;
        Binding residentAddressBinding;
        Binding birthdayBinding;
        Binding hiredDateBinding;
        Binding identifyNumberBinding;
        Binding telephoneBinding;
        Binding emailAddressBinding;        
        Binding otherBinding;


        Binding femaleBinding;
        Binding marriedBinding;
        Binding salesmenCommodityAllBinding;        


        Binding isDirtyBinding;
        Binding isDirtyBLLBinding;

        private void InitializeCommonControlBinding()
        {
            CommonMetaList commonMetaList = new CommonMetaList();


            ERmgrUIP.ListSalesmenCategoryDataTable listSalesmenCategoryDataTable = commonMetaList.GetSalesmenCategory();
            this.comboBoxSalesmenCategoryID.DataSource = listSalesmenCategoryDataTable;
            this.comboBoxSalesmenCategoryID.DisplayMember = listSalesmenCategoryDataTable.DescriptionColumn.ColumnName;
            this.comboBoxSalesmenCategoryID.ValueMember = listSalesmenCategoryDataTable.SalesmenCategoryIDColumn.ColumnName;
            this.salesmenTitleIDBinding = this.comboBoxSalesmenCategoryID.DataBindings.Add("SelectedValue", this.listSalesmenNameBLL.ListSalesmenNameMaster, "SalesmenCategoryID", true);       


            ERmgrUIP.ListSalesmenTypeDataTable listSalesmenTypeDataTable = commonMetaList.GetSalesmenType();
            this.comboBoxSalesmenTypeID.DataSource = listSalesmenTypeDataTable;
            this.comboBoxSalesmenTypeID.DisplayMember = listSalesmenTypeDataTable.DescriptionColumn.ColumnName;
            this.comboBoxSalesmenTypeID.ValueMember = listSalesmenTypeDataTable.SalesmenTypeIDColumn.ColumnName;
            this.salesmenCategoryIDBinding = this.comboBoxSalesmenTypeID.DataBindings.Add("SelectedValue", this.listSalesmenNameBLL.ListSalesmenNameMaster, "SalesmenTypeID", true);

            ERmgrUIP.ListSalesmenTitleDataTable listSalesmenNameDataTable = commonMetaList.GetSalesmenTitle();
            this.comboBoxSalesmenTitleID.DataSource = listSalesmenNameDataTable;
            this.comboBoxSalesmenTitleID.DisplayMember = listSalesmenNameDataTable.DescriptionColumn.ColumnName;
            this.comboBoxSalesmenTitleID.ValueMember = listSalesmenNameDataTable.SalesmenTitleIDColumn.ColumnName;
            this.salesmenTypeIDBinding = this.comboBoxSalesmenTitleID.DataBindings.Add("SelectedValue", this.listSalesmenNameBLL.ListSalesmenNameMaster, "SalesmenTitleID", true);


            this.descriptionBinding = this.textBoxDescription.DataBindings.Add("Text", this.listSalesmenNameBLL.ListSalesmenNameMaster, "Description", true);
            this.descriptionZULBinding = this.textBoxDescriptionZUL.DataBindings.Add("Text", this.listSalesmenNameBLL.ListSalesmenNameMaster, "DescriptionZUL", true);
            this.descriptionDEIBinding = this.textBoxDescriptionDEI.DataBindings.Add("Text", this.listSalesmenNameBLL.ListSalesmenNameMaster, "DescriptionDEI", true);
            this.descriptionOfficialBinding = this.textBoxDescriptionOfficial.DataBindings.Add("Text", this.listSalesmenNameBLL.ListSalesmenNameMaster, "DescriptionOfficial", true);
            this.descriptionLastNameBinding = this.textBoxDescriptionLastName.DataBindings.Add("Text", this.listSalesmenNameBLL.ListSalesmenNameMaster, "DescriptionLastName", true);            
            this.residentAddressBinding = this.textBoxResidentAddress.DataBindings.Add("Text", this.listSalesmenNameBLL.ListSalesmenNameMaster, "ResidentAddress", true);
            this.hiredDateBinding = this.dateTimePickerBirthday.DataBindings.Add("Value", this.listSalesmenNameBLL.ListSalesmenNameMaster, "HiredDate", true);
            this.birthdayBinding = this.dateTimePickerHiredDate.DataBindings.Add("Value", this.listSalesmenNameBLL.ListSalesmenNameMaster, "Birthday", true);
            this.identifyNumberBinding = this.textBoxIdentifyNumber.DataBindings.Add("Text", this.listSalesmenNameBLL.ListSalesmenNameMaster, "IdentifyNumber", true);
            this.telephoneBinding = this.textBoxTelephone.DataBindings.Add("Text", this.listSalesmenNameBLL.ListSalesmenNameMaster, "Telephone", true);
            this.emailAddressBinding = this.textBoxEMailAddress.DataBindings.Add("Text", this.listSalesmenNameBLL.ListSalesmenNameMaster, "EMail", true);
            this.otherBinding = this.textBoxOther.DataBindings.Add("Text", this.listSalesmenNameBLL.ListSalesmenNameMaster, "Other", true);

            this.femaleBinding = this.checkBoxFemale.DataBindings.Add("Checked", this.listSalesmenNameBLL.ListSalesmenNameMaster, "Female", true);
            this.marriedBinding = this.checkBoxMarried.DataBindings.Add("Checked", this.listSalesmenNameBLL.ListSalesmenNameMaster, "Married", true);
            this.salesmenCommodityAllBinding = this.checkBoxSalesmenCommodityAll.DataBindings.Add("Checked", this.listSalesmenNameBLL.ListSalesmenNameMaster, "SalesmenCommodityAll", true);

            this.isDirtyBinding = this.checkBoxIsDirty.DataBindings.Add("Checked", this.listSalesmenNameBLL.ListSalesmenNameMaster, "IsDirty", true);
            this.isDirtyBLLBinding = this.checkBoxIsDirtyBLL.DataBindings.Add("Checked", this.listSalesmenNameBLL, "IsDirty", true);



            this.salesmenTitleIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.salesmenCategoryIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.salesmenTypeIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
          

            this.descriptionBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.descriptionZULBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.descriptionDEIBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.descriptionOfficialBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.descriptionLastNameBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.residentAddressBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.hiredDateBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.birthdayBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.telephoneBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.emailAddressBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.identifyNumberBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.otherBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);


            this.femaleBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.marriedBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);


            this.isDirtyBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
            this.isDirtyBLLBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);

            this.numericUpDownSizingDetail.Value = 300;

            this.naviGroupDetails.DataBindings.Add("ExpandedHeight", this.numericUpDownSizingDetail, "Value", true, DataSourceUpdateMode.OnPropertyChanged);
            this.numericUpDownSizingDetail.Minimum = this.naviGroupDetails.HeaderHeight * 2;
            this.numericUpDownSizingDetail.Maximum = this.naviGroupDetails.Height + this.dataTreeListViewMaster.Height;

            this.tableLayoutPanelMaster.ColumnStyles[this.tableLayoutPanelMaster.ColumnCount - 1].SizeType = SizeType.Absolute; this.tableLayoutPanelMaster.ColumnStyles[this.tableLayoutPanelMaster.ColumnCount - 1].Width = 10;
            this.tableLayoutPanelExtend.ColumnStyles[this.tableLayoutPanelExtend.ColumnCount - 1].SizeType = SizeType.Absolute; this.tableLayoutPanelExtend.ColumnStyles[this.tableLayoutPanelExtend.ColumnCount - 1].Width = 10;

            this.errorProviderMaster.DataSource = this.listSalesmenNameBLL.ListSalesmenNameMaster;
         
        }


        private void CommonControl_BindingComplete(object sender, BindingCompleteEventArgs e)
        {
            if (e.BindingCompleteState == BindingCompleteState.Exception) { GlobalExceptionHandler.ShowExceptionMessageBox(this, e.ErrorText); e.Cancel = true; }
        }

        private void numericUpDownSizingDetail_ValueChanged(object sender, EventArgs e)
        {
            this.naviGroupDetails.Expand();
        }

        private void naviGroupDetails_HeaderMouseClick(object sender, MouseEventArgs e)
        {
            this.numericUpDownSizingDetail.Visible = this.naviGroupDetails.Expanded;
        }

        

        private void InitializeDataGridBinding()
        {

            this.GetMasterList();



            #region <dataGridViewDetail>

            DataGridViewComboBoxColumn comboBoxColumn;
            CommonMetaList commonMetaList = new CommonMetaList();

            //AddressArea

            this.dataGridViewListAddressArea.AutoGenerateColumns = false;
            listSalesmenAddressAreaView = new BindingListView<ListSalesmenAddressArea>(this.listSalesmenNameBLL.ListSalesmenAddressAreaList);
            this.dataGridViewListAddressArea.DataSource = listSalesmenAddressAreaView;

            ERmgrUIP.ListAddressAreaDataTable listAddressAreaDataTable = commonMetaList.GetAddressArea(true);

            NMVN.Common.AutoCompleteComboBoxColumn AutoCompleteComboBoxColumn;

            AutoCompleteComboBoxColumn = (NMVN.Common.AutoCompleteComboBoxColumn)this.dataGridViewListAddressArea.Columns[listAddressAreaDataTable.AddressAreaIDColumn.ColumnName];
            AutoCompleteComboBoxColumn.DataSource = listAddressAreaDataTable;
            AutoCompleteComboBoxColumn.DisplayMember = listAddressAreaDataTable.DescriptionColumn.ColumnName;
            AutoCompleteComboBoxColumn.ValueMember = listAddressAreaDataTable.AddressAreaIDColumn.ColumnName;


 
            //Commodity

            this.dataGridViewListCommodity.AutoGenerateColumns = false;
            listSalesmenCommodityView = new BindingListView<ListSalesmenCommodity>(this.listSalesmenNameBLL.ListSalesmenCommodityList);
            this.dataGridViewListCommodity.DataSource = listSalesmenCommodityView;


            ListItemCommodityTableAdapter listItemCommodityTableAdapter = new ListItemCommodityTableAdapter();
            ListSalesmenNameDTS.ListItemCommodityDataTable listItemCommodityDataTable = listItemCommodityTableAdapter.GetData();
            listItemCommodityDataTable.AddListItemCommodityRow("", "", "", "", "", "");
            
            comboBoxColumn = (DataGridViewComboBoxColumn)this.dataGridViewListCommodity.Columns[listItemCommodityDataTable.CommodityIDColumn.ColumnName];
            comboBoxColumn.DataSource = listItemCommodityDataTable;
            comboBoxColumn.DisplayMember = listItemCommodityDataTable.DescriptionColumn.ColumnName;
            comboBoxColumn.ValueMember = listItemCommodityDataTable.CommodityIDColumn.ColumnName;

            comboBoxColumn = (DataGridViewComboBoxColumn)this.dataGridViewListCommodity.Columns[listItemCommodityDataTable.ItemBrandNameColumn.ColumnName];
            comboBoxColumn.DataSource = listItemCommodityDataTable;
            comboBoxColumn.DisplayMember = listItemCommodityDataTable.ItemBrandNameColumn.ColumnName;
            comboBoxColumn.ValueMember = listItemCommodityDataTable.CommodityIDColumn.ColumnName;

            comboBoxColumn = (DataGridViewComboBoxColumn)this.dataGridViewListCommodity.Columns[listItemCommodityDataTable.ItemTypeNameColumn.ColumnName];
            comboBoxColumn.DataSource = listItemCommodityDataTable;
            comboBoxColumn.DisplayMember = listItemCommodityDataTable.ItemTypeNameColumn.ColumnName;
            comboBoxColumn.ValueMember = listItemCommodityDataTable.CommodityIDColumn.ColumnName;
            

            //Customer

            this.dataGridViewListCustomer.AutoGenerateColumns = false;
            listSalesmenCustomerView = new BindingListView<ListSalesmenCustomer>(this.listSalesmenNameBLL.ListSalesmenCustomerList);
            this.dataGridViewListCustomer.DataSource = listSalesmenCustomerView;

                                      
            ListCustomerNameTableAdapter listCustomerNameTableAdapter = new ListCustomerNameTableAdapter();
            ListSalesmenNameDTS.ListCustomerNameDataTable listCustomerNameDataTable = listCustomerNameTableAdapter.GetData();
            listCustomerNameDataTable.AddListCustomerNameRow("", "", "", "", "", "");

            //NMVN.Common.AutoCompleteComboBoxColumn AutoCompleteComboBoxColumn;

            AutoCompleteComboBoxColumn = (NMVN.Common.AutoCompleteComboBoxColumn)this.dataGridViewListCustomer.Columns[listCustomerNameDataTable.CustomerIDColumn.ColumnName];
            AutoCompleteComboBoxColumn.DataSource = listCustomerNameDataTable;
            AutoCompleteComboBoxColumn.DisplayMember = listCustomerNameDataTable.CustomerNameColumn.ColumnName;
            AutoCompleteComboBoxColumn.ValueMember = listCustomerNameDataTable.CustomerIDColumn.ColumnName;


            comboBoxColumn = (DataGridViewComboBoxColumn)this.dataGridViewListCustomer.Columns[listCustomerNameDataTable.CustomerCodeColumn.ColumnName];
            comboBoxColumn.DataSource = listCustomerNameDataTable;
            comboBoxColumn.DisplayMember = listCustomerNameDataTable.CustomerCodeColumn.ColumnName;
            comboBoxColumn.ValueMember = listCustomerNameDataTable.CustomerIDColumn.ColumnName;

            comboBoxColumn = (DataGridViewComboBoxColumn)this.dataGridViewListCustomer.Columns[listCustomerNameDataTable.DescriptionZuelligColumn.ColumnName];
            comboBoxColumn.DataSource = listCustomerNameDataTable;
            comboBoxColumn.DisplayMember = listCustomerNameDataTable.DescriptionZuelligColumn.ColumnName;
            comboBoxColumn.ValueMember = listCustomerNameDataTable.CustomerIDColumn.ColumnName;

            comboBoxColumn = (DataGridViewComboBoxColumn)this.dataGridViewListCustomer.Columns[listCustomerNameDataTable.DescriptionDiethelmColumn.ColumnName];
            comboBoxColumn.DataSource = listCustomerNameDataTable;
            comboBoxColumn.DisplayMember = listCustomerNameDataTable.DescriptionDiethelmColumn.ColumnName;
            comboBoxColumn.ValueMember = listCustomerNameDataTable.CustomerIDColumn.ColumnName;

            comboBoxColumn = (DataGridViewComboBoxColumn)this.dataGridViewListCustomer.Columns[listCustomerNameDataTable.CustomerChannelColumn.ColumnName];
            comboBoxColumn.DataSource = listCustomerNameDataTable;
            comboBoxColumn.DisplayMember = listCustomerNameDataTable.CustomerChannelColumn.ColumnName;
            comboBoxColumn.ValueMember = listCustomerNameDataTable.CustomerIDColumn.ColumnName;  

            //ItemClass

            this.dataGridViewListItemClass.AutoGenerateColumns = false;
            listSalesmenItemClassView = new BindingListView<ListSalesmenItemClass>(this.listSalesmenNameBLL.ListSalesmenItemClassList);
            this.dataGridViewListItemClass.DataSource = listSalesmenItemClassView;

            ERmgrUIP.ListItemClassDataTable listItemClassDataTable = commonMetaList.GetItemClass(true);
            AutoCompleteComboBoxColumn = (NMVN.Common.AutoCompleteComboBoxColumn)this.dataGridViewListItemClass.Columns[listItemClassDataTable.ItemClassIDColumn.ColumnName];
            AutoCompleteComboBoxColumn.DataSource = listItemClassDataTable;
            AutoCompleteComboBoxColumn.DisplayMember = listItemClassDataTable.DescriptionColumn.ColumnName;
            AutoCompleteComboBoxColumn.ValueMember = listItemClassDataTable.ItemClassIDColumn.ColumnName;



            #endregion <dataGridViewDetail>
        }


        private void InitializeReadOnlyModeBinding()
        {
            List<Control> controlList = GlobalStaticFunction.GetAllControls(this);

            foreach (Control control in controlList)
            {
                //if (control is TextBox) control.DataBindings.Add("Readonly", this, "ReadonlyMode");
                if (control is TextBox) control.DataBindings.Add("Enabled", this, "EditableMode");
                else if (control is ComboBox || control is DateTimePicker || control is CheckBox) control.DataBindings.Add("Enabled", this, "EditableMode");
                else if (control is DataGridView)
                {
                    control.DataBindings.Add("Readonly", this, "ReadonlyMode");
                    control.DataBindings.Add("AllowUserToAddRows", this, "EditableMode");
                    control.DataBindings.Add("AllowUserToDeleteRows", this, "EditableMode");
                }
            }

            this.dataTreeListViewMaster.DataBindings.Add("Enabled", this, "ReadonlyMode");
        }

        BindingListView<ListSalesmenAddressArea> listSalesmenAddressAreaView;
        BindingListView<ListSalesmenCommodity> listSalesmenCommodityView;
        BindingListView<ListSalesmenCustomer> listSalesmenCustomerView;
        BindingListView<ListSalesmenItemClass> listSalesmenItemClassView;


        private enum ListingOptions
        {
            AddressArea = 1,
            SalesmenCategory = 2           
        }


        private void GetMasterList()
        {
            try
            {
                DataTable listSalesmenNameListingDataTable;            

                listSalesmenNameListingDataTable = this.listSalesmenNameBLL.ListSalesmenNameListing();

                listSalesmenNameListingDataTable.TableName = "ListSalesmenAdressAreaListingDataTable";

                DataSet bindingDataSet = new DataSet();
                bindingDataSet.Tables.Add(listSalesmenNameListingDataTable);

                this.dataTreeListViewMaster.FullRowSelect = true;
                this.dataTreeListViewMaster.KeyAspectName = "SalesmenID";
                this.dataTreeListViewMaster.ParentKeyAspectName = "AncestorID";
                this.dataTreeListViewMaster.RootKeyValue = 0;

                this.dataTreeListViewMaster.DataMember = "ListSalesmenAdressAreaListingDataTable";
                this.dataTreeListViewMaster.DataSource = new DataViewManager(bindingDataSet);

                this.dataTreeListViewMaster.ExpandAll();
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }

        #endregion <InitializeBinding>





        #region Import Excel

        private void ImportExcel()
        {
            try
            {

            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }

        }
        #endregion Import Excel



        private void dataTreeListViewMaster_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                ObjectListView objectListView = (ObjectListView)sender;
                DataRowView row = (DataRowView)objectListView.SelectedObject;

                if (row != null)
                {
                    bool isSalesmen;  int salesmenTypeID;

                    if (bool.TryParse(row.Row["isSalesmen"].ToString(), out isSalesmen))
                        if (isSalesmen && int.TryParse(row.Row["SalesmenID"].ToString(), out salesmenTypeID)) this.listSalesmenNameBLL.Fill(salesmenTypeID);
                        else this.listSalesmenNameBLL.Fill(0);
                }
                else this.listSalesmenNameBLL.Fill(0);
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }

        private void dataTreeListViewMaster_DoubleClick(object sender, EventArgs e)
        {
            this.naviGroupDetails.Expanded = true;
        }

        private void dataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            GlobalExceptionHandler.ShowExceptionMessageBox(this, e.Exception);
        }

        private void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked)
                this.GetMasterList();
        }

        private void toolStripButtonShowDetailsExtend_Click(object sender, EventArgs e)
        {
            this.naviGroupDetailsExtend.Expanded = !this.naviGroupDetailsExtend.Expanded;
            this.toolStripButtonShowDetailsExtend.Image = this.naviGroupDetailsExtend.Expanded ? ResourceIcon.Chevron_Collapse.ToBitmap() : ResourceIcon.Chevron_Expand.ToBitmap();
        }


        private void dataGridViewListCustomer_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (this.EditableMode && this.dataGridViewListCustomer.CurrentCell != null && this.dataGridViewListCustomer.IsCurrentCellDirty)
            {
                if (this.dataGridViewListCustomer.CurrentCell.OwningColumn.Name == "CustomerID")
                {
                    this.dataGridViewListCustomer.CommitEdit(DataGridViewDataErrorContexts.Commit);
                    this.dataGridViewListCustomer.Refresh();
                }
            }
        }

        private void dataGridViewListCommodity_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (this.EditableMode && this.dataGridViewListCommodity.CurrentCell != null && this.dataGridViewListCommodity.IsCurrentCellDirty)
            {
                if (this.dataGridViewListCommodity.CurrentCell.OwningColumn.Name == "CommodityID")
                {
                    this.dataGridViewListCommodity.CommitEdit(DataGridViewDataErrorContexts.Commit);
                    this.dataGridViewListCommodity.Refresh();
                }
            }
        }

        private void checkBoxSalesmenCommodityAll_CheckStateChanged(object sender, EventArgs e)
        {
            if (((CheckBox)sender).Checked && this.EditableMode)
            {
                this.listSalesmenNameBLL.ListSalesmenItemClassList.Clear();
                this.listSalesmenNameBLL.ListSalesmenCommodityList.Clear();
                this.dataGridViewListCommodity.ReadOnly = true;
                this.dataGridViewListItemClass.ReadOnly = true;
            }
            else
            {
                this.dataGridViewListCommodity.ReadOnly = false;
                this.dataGridViewListItemClass.ReadOnly = false;
            }
        }


    }




}
